package com.scwatch.core.repository.impl;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;
import com.scwatch.common.orm.LimitRequest;
import com.scwatch.common.orm.Limitable;
import com.scwatch.common.orm.QuerydslUtils;
import com.scwatch.core.domain.LiveBroadcastRoom;
import com.scwatch.core.domain.dsl.QLiveBroadcastRoom;
import com.scwatch.core.repository.plus.LiveBroadcastRoomDaoPlus;
import org.springframework.data.domain.Sort;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Collections;
import java.util.List;

/**
 * LiveBroadcastRoomDaoImpl
 * 
 * @author gjl
 * 
 */
public class LiveBroadcastRoomDaoImpl implements LiveBroadcastRoomDaoPlus {

	private EntityManager em;

	@PersistenceContext
	public void setEm(EntityManager em) {
		this.em = em;
	}

    public List<LiveBroadcastRoom> getRoomListByLiveId(Integer liveId) {
        if (liveId==null) {
            return Collections.emptyList();
        }
        JPAQuery<LiveBroadcastRoom> query = new JPAQuery<LiveBroadcastRoom>(this.em);
//        query.setHint(QueryHints.HINT_CACHEABLE, true);
        QLiveBroadcastRoom room = QLiveBroadcastRoom.liveBroadcastRoom;
        query.from(room);
        BooleanBuilder exp = new BooleanBuilder();
        exp = exp.and(room.liveId.eq(liveId));
        query.where(exp);

        Sort defSort = new Sort(Sort.Direction.ASC, "seq", "id");
        Limitable limitable =new LimitRequest(0,99999, Sort.Direction.DESC,"time");

        QLiveBroadcastRoom liveBroadcastRoom = QLiveBroadcastRoom.liveBroadcastRoom;

        return QuerydslUtils.list(query,liveBroadcastRoom,limitable);
    }
}
